<?php
namespace App\Admin\Models;
use App\Base\Models\BaseModel;
use App\Weixin\Models\WeixinUserIsvipModel;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
/**
 * Created by 荣耀电竞.
 * User: 用户层级 <278805354@qq.com>
 * Date: 2018/5/17 0017
 * Time: 17:33
 */
class  MemberLevelModel extends  BaseModel {


    use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table="member_level";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps=true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    protected $appends = ["level_user_num", "vip_user_num"];


    /**
     * 获取用户接口
     * @return WeixinUserModel
     */
    public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
    }

    /**
     * 获取vip
     * @return WeixinUserIsvipModel
     */
    public function getWeixinUserIsvipModel(){
        return app()->make(WeixinUserIsvipModel::class);
    }

    /**
     * 统计总人数
     * @return int
     */
    public function getLevelUserNumAttribute(){
        $lid=$this->id;
        //$user_count = Cache::remember('member:level:user_' . $lid, 3600, function () use ($lid) {
            return  $this->getWeixinUserModel()->newQuery()->where("lid",$lid)->count();
        //});
        return $user_count;
    }
    /**
     * 统计Vip总人数
     */
    public function getVipUserNumAttribute(){
        $lid=$this->id;
        //$user_count = Cache::remember('member:level:user_start_' . $lid, 3600, function () use ($lid) {
            //时间在两个字段之间
            $time=time();
            return  $this->getWeixinUserIsvipModel()->newQuery()->where("lid",$lid)->where("start_time","<=",$time)->where("end_time",">=",$time)->count();
        //});
        return $user_count;

    }


}